package com.applink.sell_proof.broadcast_receiver;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.applink.sell_proof.BasePagerActivity;
import com.applink.sell_proof.DealDetailActivity;
import com.applink.sell_proof.LearnActivity;
import com.applink.sell_proof.LifeStyleActivity;
import com.applink.sell_proof.R;
import com.applink.sell_proof.constant.Constant;
import com.applink.sell_proof.constant.NotificationKey;
import com.applink.sell_proof.dialog.BirthDay;
import com.applink.sell_proof.model.Quiz;
import com.applink.sell_proof.model.Tip;

public class NotificationReceiver extends BroadcastReceiver {

	private static final String TAG = "NotificationReceiver";

	@Override
	public void onReceive(Context context, Intent intent) {
		String type = intent.getStringExtra(NotificationKey.TYPE);
		if (type.equals(TypeNotification.DEAL_CHANGE_STAGE)) {
			String message = intent.getStringExtra(NotificationKey.MESSAGE);
			long realId = Long.parseLong(intent
					.getStringExtra(NotificationKey.DEAL_ID));
			int stage = Integer.parseInt(intent
					.getStringExtra(NotificationKey.DEAL_STAGE));
			int icon = getIcon();
			long when = System.currentTimeMillis();
			NotificationManager notificationManager = (NotificationManager) context
					.getSystemService(Context.NOTIFICATION_SERVICE);
			Notification notification = new Notification(icon, message, when);
			String title = context.getString(R.string.app_name);
			Intent notificationIntent = new Intent(context,
					DealDetailActivity.class);
			notificationIntent.putExtra(Constant.INTENT_ID_DEAL, realId);
			notificationIntent.putExtra(Constant.INTENT_STAGE_DEAL, stage);
			// set intent so it does not start a new activity
			notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
					| Intent.FLAG_ACTIVITY_SINGLE_TOP);
			PendingIntent intentPending = PendingIntent.getActivity(context, 0,
					notificationIntent, 0);
			notification.setLatestEventInfo(context, title, message,
					intentPending);
			notification.flags |= Notification.FLAG_AUTO_CANCEL;
			notificationManager.notify(0, notification);
		} else if (type.equals(TypeNotification.BIRTHDAY)) {
			String message = intent.getStringExtra(NotificationKey.MESSAGE);
			int icon = getIcon();
			long when = System.currentTimeMillis();
			NotificationManager notificationManager = (NotificationManager) context
					.getSystemService(Context.NOTIFICATION_SERVICE);
			Notification notification = new Notification(icon, message, when);
			String title = context.getString(R.string.app_name);
			Intent notificationIntent = new Intent(context, BirthDay.class);
			// set intent so it does not start a new activity
			notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
					| Intent.FLAG_ACTIVITY_SINGLE_TOP);
			PendingIntent intentPending = PendingIntent.getActivity(context, 0,
					notificationIntent, 0);
			notification.setLatestEventInfo(context, title, message,
					intentPending);
			notification.flags |= Notification.FLAG_AUTO_CANCEL;
			notificationManager.notify(0, notification);
		} else if (type.equals(TypeNotification.TIP)) {
			String message = intent.getStringExtra(NotificationKey.MESSAGE);
			String category = intent
					.getStringExtra(NotificationKey.TIP_CATEGORY);
			Tip.Category tip_category = Tip.Category.getCategory(Integer
					.parseInt(category));
			int icon = getIcon();
			long when = System.currentTimeMillis();
			NotificationManager notificationManager = (NotificationManager) context
					.getSystemService(Context.NOTIFICATION_SERVICE);
			Notification notification = new Notification(icon, message, when);
			String title = context.getString(R.string.app_name);
			Intent notificationIntent;
			if (tip_category == Tip.Category.LEARN) {
				notificationIntent = new Intent(context, LearnActivity.class);
			} else {
				notificationIntent = new Intent(context,
						LifeStyleActivity.class);
			}
			notificationIntent.putExtra(BasePagerActivity.PAGER_INDEX, 0);
			// set intent so it does not start a new activity
			notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
					| Intent.FLAG_ACTIVITY_SINGLE_TOP);
			PendingIntent intentPending = PendingIntent.getActivity(context, 0,
					notificationIntent, 0);
			notification.setLatestEventInfo(context, title, message,
					intentPending);
			notification.flags |= Notification.FLAG_AUTO_CANCEL;
			notificationManager.notify(0, notification);
		} else if (type.equals(TypeNotification.QUIZ)) {
			String message = intent.getStringExtra(NotificationKey.MESSAGE);
			String category = intent
					.getStringExtra(NotificationKey.QUIZ_CATEGORY);
			Quiz.Category quiz_category = Quiz.Category.getCategory(Integer
					.parseInt(category));
			int icon = getIcon();
			long when = System.currentTimeMillis();
			NotificationManager notificationManager = (NotificationManager) context
					.getSystemService(Context.NOTIFICATION_SERVICE);
			Notification notification = new Notification(icon, message, when);
			String title = context.getString(R.string.app_name);
			Intent notificationIntent;
			if (quiz_category == Quiz.Category.LEARN) {
				notificationIntent = new Intent(context, LearnActivity.class);
			} else {
				notificationIntent = new Intent(context,
						LifeStyleActivity.class);
			}
			notificationIntent.putExtra(BasePagerActivity.PAGER_INDEX, 1);
			// set intent so it does not start a new activity
			notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
					| Intent.FLAG_ACTIVITY_SINGLE_TOP);
			PendingIntent intentPending = PendingIntent.getActivity(context, 0,
					notificationIntent, 0);
			notification.setLatestEventInfo(context, title, message,
					intentPending);
			notification.flags |= Notification.FLAG_AUTO_CANCEL;
			notificationManager.notify(0, notification);
		}
	}

	private int getIcon() {
		return R.drawable.defaut_app_icon;
	}

}
